#!/bin/sh

uci delete ucitrack.@openclash[-1] >/dev/null 2>&1
uci add ucitrack openclash >/dev/null 2>&1
uci set ucitrack.@openclash[-1].init=openclash >/dev/null 2>&1
uci commit ucitrack >/dev/null 2>&1
uci delete firewall.openclash >/dev/null 2>&1
uci set firewall.openclash=include >/dev/null 2>&1
uci set firewall.openclash.type=script >/dev/null 2>&1
uci set firewall.openclash.path=/var/etc/openclash.include >/dev/null 2>&1
uci set firewall.openclash.reload=1 >/dev/null 2>&1
uci commit firewall >/dev/null 2>&1

mkdir -p /etc/openclash/config >/dev/null 2>&1
mkdir -p /etc/openclash/proxy_provider >/dev/null 2>&1
mkdir -p /etc/openclash/backup >/dev/null 2>&1
mkdir -p /etc/openclash/core >/dev/null 2>&1
mkdir -p /etc/openclash/history >/dev/null 2>&1
mkdir -p /usr/share/openclash/backup >/dev/null 2>&1

cp "/etc/config/openclash" "/usr/share/openclash/backup/openclash" >/dev/null 2>&1
cp "/etc/openclash/custom/openclash_custom_rules.list" "/usr/share/openclash/backup/openclash_custom_rules.list" >/dev/null 2>&1
cp "/etc/openclash/custom/openclash_custom_rules_2.list" "/usr/share/openclash/backup/openclash_custom_rules_2.list" >/dev/null 2>&1
cp "/etc/openclash/custom/openclash_custom_hosts.list" "/usr/share/openclash/backup/openclash_custom_hosts.list" >/dev/null 2>&1
cp "/etc/openclash/custom/openclash_custom_fake_filter.list" "/usr/share/openclash/backup/openclash_custom_fake_filter.list" >/dev/null 2>&1
cp "/etc/openclash/custom/openclash_custom_domain_dns.list" "/usr/share/openclash/backup/openclash_custom_domain_dns.list" >/dev/null 2>&1

mkdir -p /lib/upgrade/keep.d >/dev/null 2>&1
cat > "/lib/upgrade/keep.d/luci-app-openclash" <<-EOF
/etc/openclash/
EOF

if [ -f "/tmp/openclash.bak" ]; then
	mv "/tmp/openclash.bak" "/etc/config/openclash" >/dev/null 2>&1
	cp -rf "/tmp/openclash/." "/etc/openclash/" >/dev/null 2>&1
	cp -rf "/tmp/openclash_config/." "/etc/openclash/config/" >/dev/null 2>&1
	cp -rf "/tmp/openclash_core/." "/etc/openclash/core/" >/dev/null 2>&1
	cp -rf "/tmp/openclash_history/." "/etc/openclash/history/" >/dev/null 2>&1
	cp -rf "/tmp/openclash_proxy_provider/." "/etc/openclash/proxy_provider/" >/dev/null 2>&1
	cp -rf "/tmp/openclash_rule_provider/." "/etc/openclash/rule_provider/" >/dev/null 2>&1
	cp -rf "/tmp/openclash_game_rules/." "/etc/openclash/game_rules/" >/dev/null 2>&1
	mv "/tmp/openclash_custom_rules.list.bak" "/etc/openclash/custom/openclash_custom_rules.list" >/dev/null 2>&1
	mv "/tmp/openclash_custom_rules_2.list.bak" "/etc/openclash/custom/openclash_custom_rules_2.list" >/dev/null 2>&1
	mv "/tmp/openclash_custom_hosts.list.bak" "/etc/openclash/custom/openclash_custom_hosts.list" >/dev/null 2>&1
	mv "/tmp/openclash_custom_fake_filter.list.bak" "/etc/openclash/custom/openclash_custom_fake_filter.list" >/dev/null 2>&1
	mv "/tmp/openclash_custom_domain_dns.list.bak" "/etc/openclash/custom/openclash_custom_domain_dns.list" >/dev/null 2>&1
	rm -rf "/etc/openclash/openclash" >/dev/null 2>&1
	rm -rf "/etc/openclash/fake_filter.list" >/dev/null 2>&1
	rm -rf "/etc/openclash/openclash_servers_fake_filter.conf" >/dev/null 2>&1
	rm -rf /tmp/openclash* >/dev/null 2>&1
fi

uci set openclash.config.enable=0 >/dev/null 2>&1
uci commit openclash >/dev/null 2>&1

rm -f /tmp/luci-indexcache
exit 0